package com.ciwong.epaper.common.guidedialog;

import android.app.Activity;
import android.view.View;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.modules.epaper.bean.ModuleContent;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.evaluate.ui.SpeechPlayController;
import com.ciwong.epaper.modules.evaluate.util.SpeechUtils;
import com.ciwong.epaper.util.download.DownLoadInfo;
import com.ciwong.mobilelib.i.ViewOnClickListener;

/**
 * 选择模式弹出框 Created by res-zwyan on 2015/8/11.
 */
public class ChooseReadDialog extends BaseGuideLayerDialog {

    private DownLoadInfo mDownLoadInfo;

    private Module module;

    private int position;

    private String workId;

    Activity activity;

    private String resourceType;

    private int requestCode;

    private int mServiceId;
    private String classId;
    private long effectiveDate = -1;

    public ChooseReadDialog(Activity context, String workId,
                            DownLoadInfo mDownLoadInfo, Module module, int position,
                            String resourceType, int requestCode, int serviceId,long effectiveDate) {
        this(context, workId, mDownLoadInfo, module, position, resourceType, requestCode, serviceId, null, effectiveDate);
    }

    public ChooseReadDialog(Activity context, String workId,
                            DownLoadInfo mDownLoadInfo, Module module, int position,
                            String resourceType, int requestCode, int serviceId, String classid,long effectiveDate) {
        super(context, R.style.read_choose_dialog);
        try {
            this.resourceType = resourceType;
            mServiceId = serviceId;

            setContentView(resourceType.equals(ModuleContent.ResourceType.RESOURCE_TYPE_LESSON) ? R.layout.dialog_choose_read
                    : R.layout.dialog_choose_word_read);
            setCanceledOnTouchOutside(true);
            activity = context;
            this.workId = workId;
            this.mDownLoadInfo = mDownLoadInfo;
            this.module = module;
            this.position = position;
            this.requestCode = requestCode;
            this.effectiveDate = effectiveDate;
            initEvent();
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

    private void initEvent() {
        if (resourceType
                .equals(ModuleContent.ResourceType.RESOURCE_TYPE_LISTEN_SPEAK)) {
            findViewById(R.id.dialog_own_practise)
                    .setOnClickListener(clickListener);
            findViewById(R.id.dialog_simulate_exam)
                    .setOnClickListener(clickListener);
        } else if (resourceType
                .equals(ModuleContent.ResourceType.RESOURCE_TYPE_LESSON)) {
            findViewById(R.id.dialog_read_one_by_one_tv)
                    .setOnClickListener(clickListener);
            findViewById(R.id.dialog_read_by_all_tv)
                    .setOnClickListener(clickListener);
            findViewById(R.id.dialog_read_a_passage_tv)
                    .setOnClickListener(clickListener);
        } else {
            findViewById(R.id.dialog_read_word_repeat).setOnClickListener(clickListener);
            findViewById(R.id.dialog_read_word_through).setOnClickListener(clickListener);
        }
    }

    @Override
    public void show() {
        getWindow().setWindowAnimations(R.style.read_choose_dialog);
        super.show();
    }

    private ViewOnClickListener clickListener = new ViewOnClickListener() {
        @Override
        public void avertRepeatOnClick(View v) {
            int id = v.getId();
            int speekType = 0;
            if (resourceType.equals(ModuleContent.ResourceType.RESOURCE_TYPE_LESSON)) {
                // 逐句跟读
                if (id == R.id.dialog_read_one_by_one_tv) {
                    speekType = SpeechPlayController.LISTENER_MODE_ZHUJU;
                }
                // 独立通读
                else if (id == R.id.dialog_read_by_all_tv) {
                    speekType = SpeechPlayController.LISTENER_MODE_DULI;
                }
                // 选段背诵
                else if (id == R.id.dialog_read_a_passage_tv) {
                    speekType = SpeechPlayController.LISTENER_MODE_XUANDUAN;
                }
                //这里取得当前的   如果是作业的应该是作业的classid

                if (classId == null && null != EApplication.getInstance().getClazz()) {
                    classId = String.valueOf(EApplication.getInstance().getClazz().getClassId());
                }
                EpaperJumpManager.jumpToSentenceRead(R.string.go_back,
                        activity, workId, mDownLoadInfo, module, null,
                        position, speekType, requestCode, mServiceId, classId, 0, null,effectiveDate);
            } else {
                // 单词跟读
                if (id == R.id.dialog_read_word_repeat) {
                    speekType = SpeechUtils.WordReadMode.READ_REPEAT;
                }
                // 单词通读
                else if (id == R.id.dialog_read_word_through) {
                    speekType = SpeechUtils.WordReadMode.READ_THROUGH;
                }

                //这里取得当前的   如果是作业的应该是作业的classid
                if (classId == null && null != EApplication.getInstance().getClazz()) {
                    classId = String.valueOf(EApplication.getInstance().getClazz().getClassId());
                }
                EpaperJumpManager.jumpToWordRead(R.string.go_back, activity, workId, mDownLoadInfo, module, null, position, speekType, requestCode, classId, null, mServiceId, false, null,effectiveDate);

            }
            ChooseReadDialog.this.dismiss();
        }
    };
}
